package com.wenx.v3system.modular.cloud.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenx.v3system.modular.cloud.service.SysUserTeamService;
import com.wenx.v3system.modular.cloud.domain.po.SysUserTeam;
import com.wenx.v3system.modular.cloud.mapper.SysUserTeamMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统用户团队服务实现类
 * 
 * @author wenx
 */
@Slf4j
@Service
public class SysUserTeamServiceImpl extends ServiceImpl<SysUserTeamMapper, SysUserTeam> implements SysUserTeamService {

    @Override
    public List<Long> getTeamIdsByUserId(Long userId) {
        LambdaQueryWrapper<SysUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserTeam::getUserId, userId);
        List<SysUserTeam> userTeams = list(wrapper);
        return userTeams.stream()
                .map(SysUserTeam::getTeamId)
                .collect(Collectors.toList());
    }

    @Override
    public List<Long> getUserIdsByTeamId(Long teamId) {
        LambdaQueryWrapper<SysUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserTeam::getTeamId, teamId);
        List<SysUserTeam> userTeams = list(wrapper);
        return userTeams.stream()
                .map(SysUserTeam::getUserId)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void assignTeamsToUser(Long userId, List<Long> teamIds) {
        // 先删除用户的现有团队关联
        removeUserTeams(userId);
        
        // 添加新的团队关联
        if (CollUtil.isNotEmpty(teamIds)) {
            List<SysUserTeam> userTeams = teamIds.stream()
                    .map(teamId -> {
                        SysUserTeam userTeam = new SysUserTeam();
                        userTeam.setUserId(userId);
                        userTeam.setTeamId(teamId);
                        return userTeam;
                    })
                    .collect(Collectors.toList());
            saveBatch(userTeams);
            log.info("用户团队分配成功: userId={}, teamIds={}", userId, teamIds);
        } else {
            log.info("清空用户团队关联: userId={}", userId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeUserTeams(Long userId) {
        LambdaQueryWrapper<SysUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserTeam::getUserId, userId);
        // 使用物理删除避免唯一约束冲突
        int deletedCount = baseMapper.delete(wrapper);
        log.info("物理删除用户团队关联记录数: {}, 用户ID: {}", deletedCount, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeTeamUsers(Long teamId) {
        LambdaQueryWrapper<SysUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserTeam::getTeamId, teamId);
        // 使用物理删除避免唯一约束冲突
        int deletedCount = baseMapper.delete(wrapper);
        log.info("物理删除团队用户关联记录数: {}, 团队ID: {}", deletedCount, teamId);
    }

    @Override
    public boolean isUserInTeam(Long userId, Long teamId) {
        LambdaQueryWrapper<SysUserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserTeam::getUserId, userId)
                .eq(SysUserTeam::getTeamId, teamId);
        return count(wrapper) > 0;
    }
}